<template>
  <div class="app-container">
    <cus-wraper>
      <cus-filter-wraper>
        <div>
          <el-input v-model="searchVal" clearable onkeyup="value=value.replace(/[^\d]/g,'')"
                    placeholder="请输入公会Num" style="width:200px;margin: 0 10px;"
          ></el-input>
          <el-button icon="el-icon-search" type="primary" @click="search">查询</el-button>
          <el-button icon="el-icon-search" type="info" @click="reGetList">重置</el-button>
        </div>
        <el-button icon="el-icon-search" type="primary" @click="handleCreate">创建</el-button>
      </cus-filter-wraper>
      <div class="table-container">
        <el-table ref="itsmDataTable" v-loading="listLoading" :data="list" align="center" element-loading-text="Loading"
                  fit
                  highlight-current-row size="mini"
        >
          <!--          <el-table-column type="selection" width="45"></el-table-column>-->
          <el-table-column align="center" label="名称" prop="groupName"></el-table-column>
          <el-table-column align="center" label="图片" prop="groupPicture">
            <template slot-scope="scope">
              <span>
                <img :src="scope.row.groupPicture" alt="" style="width: 50px;">
              </span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="开关" prop="groupSwitch">
            <template slot-scope="scope">
              <el-button v-if="scope.row.groupSwitch === 1" round type="success">已启用</el-button>
              <el-button v-else round type="danger">已禁用</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="排序" prop="groupOrder"></el-table-column>
          <el-table-column v-if="this.global_checkBtnPermission(['sys:user:edit','sys:user:delete'])" align="center"
                           label="操作"
          >
            <template slot-scope="scope">
              <el-button icon="el-icon-edit" plain size="mini" type="primary" @click="handleUpdate(scope.row)">
                编辑
              </el-button>
              <cus-del-btn @ok="handleDelete(scope.row)"/>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination v-show="total>0" :limit.sync="listQuery.pageSize" :page.sync="listQuery.page" :total="total"
                        @pagination="getList"
        />
      </div>

      <el-dialog v-dialogDrag :title="titleMap[dialogStatus]" :visible.sync="dialogVisible" width="40%"
                 @close="handleDialogClose"
      >
        <el-form ref="dataForm" :model="form" :rules="rules" class="demo-ruleForm" label-width="120px">
          <el-form-item label="名称:" prop="username">
            <el-input v-model="form.groupName"></el-input>
          </el-form-item>
          <el-form-item label="图片:" prop="username">

            <el-upload
              ref="upload"
              :auto-upload="true"
              :class="{disabled:uploadDisabled}"
              :http-request="customizeUpload"
              :limit="1"
              :on-change="handleLimit"
              :on-remove="handleRemove"
              :on-success="form.groupPicture = ossLastImgUrl"
              :style="{display:form.groupPicture?'none':'block'}"
              action=""
              list-type="picture-card"
            >
              <i class="el-icon-plus"/>
            </el-upload>
            <img :src="form.groupPicture" :style="{display:form.groupPicture?'block':'none'}" alt="" class="uploadImg">
            <el-button :style="{display:form.groupPicture?'block':'none'}" class="reload" type="primary"
                       @click="reload(1)"
            >重新上传
            </el-button>
          </el-form-item>

          <el-form-item label="开关:" prop="groupSwitch">
            <!--            {{form.groupSwitch}}-->
            <!--            {{form.groupOrder}}-->
            <el-select v-model="form.groupSwitch" filterable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序:" prop="groupOrder">
            <el-input-number v-model="form.groupOrder" :max="1000" :min="1" label="排序" @change="handleChange"
            ></el-input-number>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false"> 取 消 </el-button>
          <el-button type="primary" @click="submitForm"> 确 认 </el-button>
        </span>
      </el-dialog>
    </cus-wraper>
  </div>
</template>

<script>
import { addSkillGroup, deleteSysUser, getSysUserPage } from '@/api/qualification/qualification'
import { client } from '@/utils/ali-oss'

export default {
  data() {
    return {
      dialogVisible: false,
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        // id:7,
        // groupName
        page: 1,
        pageSize: 10,
        username: undefined
      },
      form: {
        id: undefined, //主键ID
        groupName: undefined, //账号
        groupPicture: undefined, //账号
        groupOrder: 1, //账号
        groupSwitch: 1 //账号
      },
      dialogStatus: '',
      titleMap: {
        update: '编辑分组',
        create: '创建分组'
      },

      dialogVisibleImg1: false,
      uploadDisabled: false,
      options: [{
        value: 1,
        label: '启用'
      }, {
        value: 0,
        label: '禁用'
      }],
      changeValue: 1,
      searchVal: '',

      rules: {
        groupName: [
          { required: true, message: '请输入表情名称', trigger: 'blur' }
        ],
        groupPicture: [
          { required: true, message: '请上传表情图片', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getSysUserPage(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    search() {
      if (this.changeValue === '1') {
        this.listQuery.id = this.searchVal
        this.listQuery.groupName = ''
      } else {
        this.listQuery.groupName = this.searchVal
        this.listQuery.id = ''
      }

      this.getList()
    },
    reGetList() {
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogVisible = true
    },
    handleUpdate(row) {
      // row)
      this.form = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogVisible = true
    },
    handleDelete(row) {
      let id = row.id
      deleteSysUser(id).then(response => {
        if (response.code === 0) {
          this.getList()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        }
      })
    },
    submitForm() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          addSkillGroup(this.form).then(response => {
            if (response.code === 0) {
              this.getList()
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              })

              this.dialogVisible = false
            } else {
              this.$message({
                message: response.message,
                type: 'error'
              })
            }
          }).catch(err => {

          })
        }
      })
    },
    resetForm() {
      this.form = {
        id: undefined, //主键ID
        groupName: undefined, //账号
        groupPicture: undefined, //账号
        groupOrder: 1, //账号
        groupSwitch: 1 //账号
      }
      if (this.$refs.upload) {
        this.$refs.upload.clearFiles()
      }
      if (this.$refs.upload2) {
        this.$refs.upload2.clearFiles()
      }
      this.uploadDisabled = false
      this.uploadDisabled2 = false
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose() {
      this.resetForm()

      if (this.$refs['dataForm']) {
        this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
      }
    },
    toggleSelection() {
      // 一键删除
      const _selectData = this.$refs.itsmDataTable.selection
      if (_selectData.length === 0) {
        return
      }
      let selectArry = []
      _selectData.map(row => {
        selectArry.push(row.id)
      })

      deleteSysUser(selectArry).then(response => {
        if (response.code === 0) {
          this.getList()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        }
      })
    },

    // 上传图片
    Upload(file) {
      const that = this
      const names = ['image/jpg', 'image/jpeg', 'image/png', 'image/gif']
      if (!names.includes(file.file.type)) {
        this.$message.error('不支持的格式!')
        return
      }
      const fileName = Date.now() + '-' + file.file.name
      client().put(fileName, file.file).then(result => {
        that.form.groupPicture = result.url
      }).catch(err => {

      })
    },
    // 删除图片
    handleRemove(fileList) {
      this.form.groupPicture = ''
      this.uploadDisabled = fileList.length >= 1
    },
    handleLimit(file, fileList) {
      this.uploadDisabled = fileList.length >= 1
    },

    reload(val) {
      this.form.groupPicture = ''
      this.uploadDisabled = false
      this.$refs.upload.clearFiles()
    },
    handleChange(value) {

    }

  }
}
</script>
<style lang="scss" scoped>
// .el-upload--picture-card 控制加号部分
.disabled /deep/ .el-upload--picture-card {
  display : none;
}

.disabled2 /deep/ .el-upload--picture-card {
  display : none;
}

.uploadImg, .uploadImg2 {
  width         : 148px;
  height        : 148px;
  border-radius : 6px;
}

.reload {
  position : absolute;
  bottom   : 0;
  left     : 160px;
}

.search-line {
  margin-bottom : 10px;
  padding       : 10px;
  background    : white;
}

.filter-wraper {
  display         : flex;
  justify-content : space-between;
}

.table-container /deep/ .el-table .cell {
  display : flex;
}
</style>
